package com.study.demo.aop.demo1_2.interceptor;

import com.study.demo.aop.demo1_2.LogOperationInvoker;
import com.study.demo.aop.demo2.ElvinOperationInvoker;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.lang.Nullable;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * @author: elvin
 * @time: 2019-10-16 13:27
 * @desc:
 **/
public class LogInterceptor extends LogAspectSupport implements MethodInterceptor, Serializable {
    @Override
    @Nullable
    public Object invoke(final MethodInvocation invocation) throws Exception {
        Method method = invocation.getMethod();

        LogOperationInvoker aopAllianceInvoker = () -> {
            try {
                //调用被代理的方法
                return invocation.proceed();
            }
            catch (Throwable ex) {
                throw new LogOperationInvoker.ThrowableWrapper(ex);
            }
        };

        try {
            return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments());
        }
        catch (Exception e) {
            throw e;
        }
    }
}
